package laji;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BlogEditorWindow extends JFrame {
    private JTextField titleTextField;
    private JTextArea contentTextArea;
    private JTextField authorTextField;
    private JButton saveButton;

    private MainWindow mainWindow;

    public BlogEditorWindow(MainWindow mainWindow) {
        this.mainWindow = mainWindow;

        setTitle("编辑博客");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setPreferredSize(new Dimension(400, 300));
        pack();
        setLocationRelativeTo(mainWindow);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 10));

        JLabel titleLabel = new JLabel("标题");
        contentPane.add(titleLabel, BorderLayout.NORTH);

        titleTextField = new JTextField();
        contentPane.add(titleTextField, BorderLayout.CENTER);

        JLabel contentLabel = new JLabel("内容");
        contentPane.add(contentLabel, BorderLayout.CENTER);

        contentTextArea = new JTextArea();
        JScrollPane contentScrollPane = new JScrollPane(contentTextArea);
        contentPane.add(contentScrollPane, BorderLayout.CENTER);

        JLabel authorLabel = new JLabel("作者");
        contentPane.add(authorLabel, BorderLayout.CENTER);

        authorTextField = new JTextField();
        contentPane.add(authorTextField, BorderLayout.CENTER);

        saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveBlog();
            }
        });
        contentPane.add(saveButton, BorderLayout.SOUTH);
    }

    private void saveBlog() {
        String title = titleTextField.getText();
        String content = contentTextArea.getText();
        String author = authorTextField.getText();

        Blog1 blog = new Blog1(title, content, author);
        mainWindow.addBlog(blog);
        dispose();
    }
}
